package org.linlinjava.litemall.db.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.linlinjava.litemall.db.dto.WebOrderGoodsRemarkDTO;
import org.linlinjava.litemall.db.handler.ListMapTypeHandler;
import org.linlinjava.litemall.db.handler.OrderGoodsRemarkListTypeHandler;
import org.linlinjava.litemall.db.mybatis.JsonIntegerArrayTypeHandler;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * litemall订货
 *
 * @author yliyun
 * @date 2022/09/22
 */
@TableName(value = "litemall_order_goods",autoResultMap = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LitemallOrderGoods {
    /**
     * 被删除
     */
    public static final Boolean IS_DELETED = Deleted.IS_DELETED.value();

    /**
     * 不删除
     */
    public static final Boolean NOT_DELETED = Deleted.NOT_DELETED.value();

    /**
     * id
     */
    @TableId
    private Integer id;

    /**
     * 订单id
     */
    private Integer orderId;

    /**
     * 商店id
     */
    private Integer shopId;

    /**
     * 商品id
     */
    private Integer goodsId;

    /**
     * 商品名称
     */
    private String goodsName;

    /**
     * 货物sn
     */
    private String goodsSn;

    /**
     * 类别id
     */
    private Integer categoryId;

    /**
     * 类别名称
     */
    private String categoryName;

    /**
     * 产品id
     */
    private Integer productId;

    /**
     * 数量
     */
    private Short number;

    /**
     * 税价格
     */
    private BigDecimal taxPrice;

    /**
     * 单价
     */
    private BigDecimal unitPrice;

    /**
     * 销售价格
     */
    private BigDecimal sellingPrice;

    /**
     * 折扣价格
     */
    private BigDecimal discountPrice;

    /**
     * 价格
     */
    private BigDecimal price;

    /**
     * 规范
     */
    private String[] specifications;

    /**
     * 照片url
     */
    private String picUrl;

    /**
     * 评论
     */
    private Integer comment;

    /**
     * 了状态
     */
    private Byte madeStatus;

    /**
     * 退款编号
     */
    private Short refundNumber;

    /**
     * 是否配方
     */
    private Boolean isFormula;

    /**
     * 添加时间
     */
    private LocalDateTime addTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 删除
     */
    private Boolean deleted;

    /**
     * 规范标识
     */
    @TableField(typeHandler = JsonIntegerArrayTypeHandler.class)
    private Integer[] specificationIds;

    /**
     * 让用户id
     */
    private Integer madeUserId;

    /**
     * 备注
     */
    @TableField(typeHandler = OrderGoodsRemarkListTypeHandler.class)
    List<WebOrderGoodsRemarkDTO> remark;

    @TableField(typeHandler = ListMapTypeHandler.class)
    private List<Map> refundTime;

    public enum Deleted {
        NOT_DELETED(new Boolean("0"), "未删除"),
        IS_DELETED(new Boolean("1"), "已删除");

        private final Boolean value;

        private final String name;

        Deleted(Boolean value, String name) {
            this.value = value;
            this.name = name;
        }

        public Boolean getValue() {
            return this.value;
        }

        public Boolean value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }
    }

    public enum Column {
        id("id", "id", "INTEGER", false),
        orderId("order_id", "orderId", "INTEGER", false),
        shopId("shop_id", "shopId", "INTEGER", false),
        goodsId("goods_id", "goodsId", "INTEGER", false),
        goodsName("goods_name", "goodsName", "VARCHAR", false),
        goodsSn("goods_sn", "goodsSn", "VARCHAR", false),
        categoryId("category_id", "categoryId", "INTEGER", false),
        categoryName("category_name", "categoryName", "VARCHAR", false),
        productId("product_id", "productId", "INTEGER", false),
        number("number", "number", "SMALLINT", true),
        taxPrice("tax_price", "taxPrice", "DECIMAL", false),
        unitPrice("unit_price", "unitPrice", "DECIMAL", false),
        sellingPrice("selling_price", "sellingPrice", "DECIMAL", false),
        discountPrice("discount_price", "discountPrice", "DECIMAL", false),
        price("price", "price", "DECIMAL", false),
        specifications("specifications", "specifications", "VARCHAR", false),
        picUrl("pic_url", "picUrl", "VARCHAR", false),
        comment("comment", "comment", "INTEGER", true),
        madeStatus("made_status", "madeStatus", "TINYINT", false),
        refundNumber("refund_number", "refundNumber", "SMALLINT", false),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        deleted("deleted", "deleted", "BIT", false),
        specificationIds("specification_ids", "specificationIds", "VARCHAR", false),
        madeUserId("made_user_id", "madeUserId", "INTEGER", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        public String value() {
            return this.column;
        }

        public String getValue() {
            return this.column;
        }

        public String getJavaProperty() {
            return this.javaProperty;
        }

        public String getJdbcType() {
            return this.jdbcType;
        }

        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        public static Column[] excludes(Column ... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
